<?php
namespace Admin\Controller;

use Common\Util\UploadHandler;
use Common\Controller\AdminbaseController;

class ServiceItemController extends AdminbaseController {
    public function index(){
    	$seach = I ('get.key');
    	$ServiceItemModel = D('ServiceItem');
    	
    	$where = array('status' => 1);
    	if ($seach) {
	    	$where ['service_name'] = array('like', '%'.$seach.'%');
    	}
    	 
    	$list = $ServiceItemModel->where($where)->select();
    	$this->assign('list', $list);
    	$this->display();
    }
    
    public function add() {
    	if (!IS_POST) {
    		$this->assign('number', md5(microtime()));
    		$this->assign('products', M('Product')->where(array('status' => 1))->select());
    		
    		$this->display();
    	} else {
    		$ServiceItemModel = D('ServiceItem');
    		if (!$ServiceItemModel->create(I('post.'))) {
    			$this->error($ServiceItemModel->getError());
    			exit();
    		} else {
    			if ($ServiceItemModel->addService(I('post.'))) {
    				$operation = array();
    				\Think\Hook::listen('operation', $operation);
    				
    				$this->success('添加成功');
    			} else {
    				$this->error($ServiceItemModel->getError());
    			}
    		}
    	}
    }
    
    public function edit() {
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$Model = D('ServiceItem');
    		$PhotoModel = D('Photo');
    		
    		$item = $Model->getServiceInfo($id);
    		$steps = D('Content')->getTargetContent($id);

    		$PhotoModel->type = 'carousel';
    		$covers = $PhotoModel->getTargetPhotos($id);
    		$products = M('Product')->where(array('status' => 1))->select();
    		
    		if ($products) {
    			$useProducts = M('service_use_product')->where(array('status' => 1, 'target_id' => $id))->select();
    			if ($useProducts) {
    				$useProducts = array_map(function ($var) {
    					return $var ['product_id'];
    				}, $useProducts);
    				
    				foreach ($products as &$value) {
    					if (in_array($value ['id'], $useProducts)) {
    						$value ['selected'] = true;
    					}
    				}
    			}
    			
    			$this->assign('products', $products);
    		}
    		
    		
    		if ($covers) {
    			$this->assign('covers', $covers);
    			$this->assign('number', $covers [0] ['upload_number']);
    		} else {
    			$this->assign('number', md5(microtime()));
    		}
    		
    		$this->assign('steps', $steps);
    		$this->assign('item', $item);
    		$this->display();
    	} else {
    		$id = I ('post.id');
    		
    		$ServiceItemModel = D('ServiceItem');
    		if (!$ServiceItemModel->updateInfo(I('post.'))) {
    			$this->error($ServiceItemModel->getError());
    		} else {
    			$operation = array('operation_object_id' => I('post.id'));
    			\Think\Hook::listen('operation', $operation);
    			
    			$this->success('编辑成功');
    		}
    	}
    }
    
    public function delete() {
    	$Model = D('ServiceItem');
    	
    	if (!IS_POST) {
    		$id = I ('get.id');
    		 
    		$result = $OrderModel->where(array('id' => $id))->save(array('status' => 0));
    		if ($result) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			$Model->clearCache($id);
    			$this->ajaxReturn(array('status' => 1, 'url' => U('index')));
    		} else {
    			$this->ajaxReturn(array('status' => 0));
    		}
    	} else {
    		$ids = I ('post.ids');
    		if (count($ids) == 1) {
    			$where = array('id' => $ids[0]);
    			$result = $Model->where($where)->save(array('status' => 0));
    		} else {
    			$where ['id'] = array('in', implode(',',$ids));
    			$result = $Model->where($where)->save(array('status' => 0));
    		}
    		 
    		if (!$result) {
    			$this->ajaxReturn(array('status' => 0 ));
    		}
    		 
    		foreach ($ids as $serviceId) {
    			$operation = array('operation_object_id' => $serviceId);
    			\Think\Hook::listen('operation', $operation);
    			$Model->clearCache($serviceId);
    		}
    		 
    		$Model->clearCache();
    		$this->ajaxReturn(array('status' => 1, 'url' => U('index')));
    	}
    }
    
    public function uploadServiceCover() {
    	$options = C('SERVICE_FILE_UPLODE_CONFIG');
    	$options ['upload_url'] = get_full_url().'/Uploads/Service/Cover/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    	$this->ajaxReturn($result [$param_name] [0]);
    }
    
    public function uploadServiceOrderBanner() {
    	$options = C('SERVICE_FILE_ORDER_BANNER_CONFIG');
    	$options ['upload_url'] = get_full_url().'/Uploads/Service/OrderBanner/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    	$this->ajaxReturn($result [$param_name] [0]);
    }
    
    public function uploadServiceBanner() {
    	$num = I('post.num');
    	$options = C('SERVICE_FILE_UPLODE_CAROUSEL_CONFIG');
    	$options ['upload_dir'] .= $num.'/';
    	$options ['upload_url'] = get_full_url().'/Uploads/Service/Carousel/'.$num.'/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    	$this->ajaxReturn($result [$param_name]);
    }
}